package com.lsw.jgyf.ui.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lsw.dialog.PubDialogUtil;
import com.lsw.jgyf.R;
import com.lsw.jgyf.model.HeadModel;
import com.lsw.jgyf.ui.activity.ChangePassWordActivity;
import com.lsw.jgyf.ui.activity.LoginActivity;
import com.lsw.jgyf.ui.activity.MyOrderActivity;
import com.lsw.jgyf.ui.activity.PersonalCenterActivity;
import com.lsw.jgyf.ui.base.fragment.BaseFragment;
import com.lsw.photoview.TakePhotoActivity;
import com.lsw.shareUser.ShareUserInfoUtil;
import com.lsw.util.AppManager;
import com.lsw.util.LogUtils;
import com.lsw.util.StringUtil;
import com.lsw.view.RoundImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

import butterknife.BindView;

/**
 * Created by lsw on 2018/7/8.
 */

public class HomeFragment4 extends BaseFragment<HomeFragment4Presenter> implements HomeFragment4Contract.View {
    //返回键
    @BindView(R.id.action_bar_left_btn)
    ImageView btnLeft;
    //标题
    @BindView(R.id.action_bar_title)
    TextView tvTitle;
    @BindView(R.id.ll_order)
    LinearLayout ll_order;
    @BindView(R.id.ll_change_pass)
    LinearLayout ll_change_pass;
    @BindView(R.id.ll_login_out)
    LinearLayout ll_login_out;
    @BindView(R.id.ll_personal_center)
    LinearLayout ll_personal_center;
    @BindView(R.id.roundImageView)
    RoundImageView roundImageView;
    PubDialogUtil dialogUtil;

    @Override
    protected int getlayoutId() {
        return R.layout.fragment_home4;
    }

    @Override
    protected void initInjector() {
        mFragmentComponent.inject(this);
    }

    @Override
    protected void initEventAndData() {
        dialogUtil = new PubDialogUtil(mContext);
        tvTitle.setText("我的");
        btnLeft.setVisibility(View.INVISIBLE);
        ll_order.setOnClickListener(v -> {
            Intent intent = new Intent(mContext, MyOrderActivity.class);
            startActivity(intent);
        });
        ll_change_pass.setOnClickListener(v -> {
            Intent intent = new Intent(mContext, ChangePassWordActivity.class);
            startActivity(intent);
        });
        ll_login_out.setOnClickListener(v -> {
            dialogUtil.showDialognotitleText("确定要退出登录？", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialogUtil.dismiss();
                    ShareUserInfoUtil.getInstance(mContext).clearCache();
                    Intent intent = new Intent(mContext, LoginActivity.class);
                    startActivity(intent);
                    getActivity().finish();
                    AppManager.getAppManager().finishAllMainActivity(LoginActivity.class);
                }
            }, true, "", false);
        });
        roundImageView.setOnClickListener(v -> {
            TakePhotoActivity.startActivityForSiglePhoto(getActivity(), 1, false);
        });
        if (!StringUtil.isNullOrEmpty(ShareUserInfoUtil.getInstance(mContext).getString(ShareUserInfoUtil.HEADPHOTO, ""))) {
            ImageLoader.getInstance().displayImage(ShareUserInfoUtil.getInstance(mContext).getString(ShareUserInfoUtil.HEADPHOTO, ""), roundImageView);
        }
        ll_personal_center.setOnClickListener(v -> {
            Intent intent = new Intent(mContext, PersonalCenterActivity.class);
            startActivity(intent);
        });
    }

    @Override
    public void upSucceed(HeadModel model) {
        if (!StringUtil.isNullOrEmpty(model.getUrl())) {
            ImageLoader.getInstance().displayImage(model.getUrl(), roundImageView);
            ShareUserInfoUtil.getInstance(mContext).setString(ShareUserInfoUtil.HEADPHOTO, model.getUrl());
        }
    }

    @Override
    protected void lazyLoadData() {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == getActivity().RESULT_OK) {
            switch (requestCode) {
                case 1:
                    String path = TakePhotoActivity.getDataPath(data);
                    LogUtils.i("lsw", "path===" + path);
                    if (!StringUtil.isNullOrEmpty(path)) {
                        mPresenter.uphead(path);
                    }
                    break;
            }
        }
    }
}
